﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


using System.Data.Entity.Core.EntityClient;
using WinnerCMS.Data;
using WinnerCMS.Model;
using WinnerCMS.Common;
namespace WinnerCMS.Data
{
    public class UserMoneyDal
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void Add(UserMoney model)
        {
            using (Entities db = new Entities())
            {
                var BalanceX = db.UserMoney.Where(x => x.UserName == model.UserName).OrderByDescending(x => x.Id).Select(x => x.Balance).DefaultIfEmpty(0).First();
                 
                if (model.CheckIn > 0)
                {
                    model.CheckOut = 0;
                }
                else
                {
                    model.CheckOut = Math.Abs(model.CheckIn.Value);
                    model.CheckIn = 0;
                }
                model.Balance = BalanceX + model.CheckIn.Value + model.CheckOut.Value;

                db.UserMoney.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 获取用户的信息
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public UserMoney GetMemberMoneysInfo(int UserID)
        {
            using (Entities db = new Entities())
            {
                return db.UserMoney.Where(x => x.UserId == UserID).OrderByDescending(x => x.Id).FirstOrDefault();
            }
        }
        /// <summary>
        /// 推荐人奖励
        /// </summary>
        /// <param name="TuiJianRen"></param>
        /// <param name="BeiTuiJianRen"></param>
        public void TuiJian(string TuiJianRen, string BeiTuiJianRen)
        {
            //EntityParameter[] arrParameter = new EntityParameter[2];
            //arrParameter[0] = new EntityParameter("TuiJianRen ", DbType.String);
            //arrParameter[0].Value = TuiJianRen;
            //arrParameter[1] = new EntityParameter("BeiTuiJianRen ", DbType.String);
            //arrParameter[1].Value = BeiTuiJianRen;

            //EFBase.ExecuteNonQuery("P_CMS_UserMoney_TuiJian", arrParameter, CommandType.StoredProcedure);
        }
    }
}
